package com.supercwn.player.app.manager.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.supercwn.player.R;
import com.supercwn.player.app.manager.bean.StorageBean;
import com.supercwn.player.app.manager.util.CleanFileManager;

import java.util.List;

/**
 * 项目名称：SuperPlayer-master
 * 类描述：
 * 创建人：TianGenhu
 * 创建时间：2017/12/11 15:13
 * 修改人：TianGenhu
 * 修改时间：2017/12/11 15:13
 * 修改备注：
 */

public class CleanFileManagerAdapter extends RecyclerView.Adapter<CleanFileManagerAdapter.AppViewHolder> {
    private Context mContext;
    private List<StorageBean> storageBeanList;

    public CleanFileManagerAdapter(Context mContext, List<StorageBean> storageBeanList) {
        this.mContext = mContext;
        this.storageBeanList = storageBeanList;
    }

    @Override
    public AppViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_app_clean_file_manager, null);
        AppViewHolder viewHolder = new AppViewHolder(view);
        viewHolder.count = (TextView) view.findViewById(R.id.count);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.date = (TextView) view.findViewById(R.id.date);
        viewHolder.size = (TextView) view.findViewById(R.id.size);
        viewHolder.fileUrl = (TextView) view.findViewById(R.id.file_url);
        view.setTag(viewHolder);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AppViewHolder holder, int position) {
        StorageBean storageBean = storageBeanList.get(position);
        long size = storageBean.getSize();
        holder.count.setText(position + "");
        holder.name.setText(storageBean.getName());
        holder.date.setText(storageBean.getTime());
        holder.size.setText(CleanFileManager.getFormatSize(size));
        String url = storageBean.getUrl();
        Log.i("TGH", "适配器中URL  ：" + url);
        holder.fileUrl.setText(url);
        holder.updata(position);

    }

    @Override
    public int getItemCount() {
        return storageBeanList == null ? 0 : storageBeanList.size();
    }


    public class AppViewHolder extends RecyclerView.ViewHolder {
        private View mView;
        private TextView name, date, size, fileUrl, count;
        private LinearLayout itemCleanFileLinearlayout;

        public AppViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            itemCleanFileLinearlayout = (LinearLayout) itemView.findViewById(R.id.item_clean_file_linearlayout);
        }

        public void updata(final int position) {
            itemCleanFileLinearlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClick != null) {
                        onItemClick.onItemClick(position, itemCleanFileLinearlayout);
                    }
                }
            });

        }
    }

    private OnItemClicks onItemClick;

    public void setOnItemClick(OnItemClicks onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClicks {
        void onItemClick(int position, LinearLayout line);
    }


}
